<?php 
Class userController extends appController
{
	
	public function index()
	{	
		$input['account']=$_SESSION['account']=hy_rand(6);
		$input['pwd']=$_SESSION['pwd']=hy_rand(6);
		$this->assign('input',$input);
		
		$app=Model('app');
	
		$id=$app->code_to_id('wardrobe');
		$cat_list=$app->getCurClass($id);
		$this->assign('cat',$cat_list);
		
		$photo_id=$app->code_to_id('rules');
		$cat_list=$app->getSubClass($photo_id);
		$this->assign('enjoy_list',$cat_list);
		
		$photo_id=$app->code_to_id('information');
		$cat_list=$app->getSubClass($photo_id);
		$this->assign('info_list',$cat_list);
		
		$this->assign_global();
		$this->display('index.html');
	
	}
	public function login()
	{
		
		$input['account']=$_SESSION['account']=hy_rand(6);
		$input['pwd']=$_SESSION['pwd']=hy_rand(6);
		
		$this->assign_global();
		$this->assign('input',$input);
	    $this->display('login.html');
	}
	public function act_reg()
	{
		$result=array('error'=>1,'content'=>'','url'=>'');
		$input['realname']=$_POST['realname'];
		$input['phone']=$_POST['phone'];
		$shopname=$_POST['shopname'];
		$shoparea=intval($_POST['shoparea']);
		$billno=strtoupper($_POST['piece_num']);
		$amount=$_POST['money'];
		$input['created']=$_SERVER['REQUEST_TIME'];
		$input['password']=$_POST['pwd']==''?substr($input['phone'],strlen($input['phone'])-4,4):$_POST['pwd'];
	   
		if($input['phone']==''||$input['realname']==''||$billno==''||$amount=='')
		{
			$result['content']='电话,姓名,小票编号和金额不能为空';
			die(json_encode($result));
		}
		
		if(hy_check($input['realname'],'ur')=='#@_error')
		{
			$result['content']='请检查姓名填写';
			die(json_encode($result));
		}
		
		if(hy_check($input['phone'],'m')=='#@_error'&&hy_check($input['phone'],'t')=='#@_error')
		{
			$result['content']='请检查电话填写';
			die(json_encode($result));
		}
		if(!preg_match('/^ALT/',$billno))
		{
			$result['content']='小票格式不正确';
			die(json_encode($result));
		}
		if(hy_check($amount,'f')=='#@_error')
		{
			$result['content']='请检查金额填写';
			die(json_encode($result));
		}
		if($amount<1000)
		{
			$result['content']='购买金额大于1000方符合申请资格';
			die(json_encode($result));
		}
		if(hy_check($input['password'],'','4-20')=='#@_error')
		{
			$result['content']='密码在4到20位之间';
			die(json_encode($result));
		}
		$user=Model('user');
		if($user->is_repeat($input['phone'],'phone'))
		{
			$result['content']='电话号码已被注册';
			die(json_encode($result));
		}
		$input['password']=md5($input['password']);
		$input['remark']= serialize(array('shopname'=>$shopname,'shoparea'=>$shoparea,'billno'=>$billno,'amount'=>$amount));
		
		if($user->insert($input,'users'))
		{
			$result['error']=0;
			$result['content']=Lang('REG_SUCCESS');
		}
		else 
		{
			$result['content']=Lang('REG_ERR');
		}
		die(json_encode($result));
	}
	public function act_login()
	{
		$result=array('error'=>1,'content'=>'','url'=>'');
		if(isset($_SESSION['account'])&&isset($_SESSION['pwd']))
		{
			
			$username=isset($_POST[$_SESSION['account']])?hy_check($_POST[$_SESSION['account']],'n',15):'';
			$password=isset($_POST[$_SESSION['pwd']])?hy_check($_POST[$_SESSION['pwd']],'',30):'';
			if($username=='#@_error'||$password=='#@_error')
			{
				$result['content']=Lang('LOGIN_ERR');
			}
			else 
			{
				if(Model('user')->check_user($username,$password))
				{
					$result['error']=0;
					$result['content']=Lang('WELCOME_BACK');
					if(REWRITE==1)
					{
						$result['url']='/user/index.html';
					}
					else 
					{
						$result['url']='/index.php?g=home&m=user&a=index';
					}
					
				}
				else 
				{
					$result['content']=Lang('LOGIN_ERR');
				}
			}
			die(json_encode($result));
		}
		else 
		{
			exit;
		}
		
	}

	public function logout($ajax=0)
	{
	
	
		cookie('QIAQIA[user_id]', '');
		cookie('QIAQIA[user_pwd]', '');
		
		
		
		session('[destroy]');
		if($ajax==0)
		{
			header('Location:/index.php');
		}
	   
	
	}
	
	
	
}